﻿Imports System
Imports System.Data
Imports MySql.Data.MySqlClient

Public Class form_penjadwalan

    Inherits DevExpress.XtraEditors.XtraForm
    Private Sub TugasBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.TugasBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DS_t01_tugas)

    End Sub

    Private Sub form_penjadwalan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DS_t01_tugas.tugas' table. You can move, or remove it, as needed.
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        DevExpress.UserSkins.OfficeSkins.Register()
        Me.LookAndFeel.SkinName = "Office 2010 Blue"
        DefaultLookAndFeel1.LookAndFeel.SkinName = "Office 2010 Blue"
        LoadNow()
    End Sub
    Function Form_Valid()
        Dim Pesan, Validasi As String
        Pesan = "" : Validasi = ""

        If TANGGAL_MULAI.Text = "" Then Pesan += "- Tanggal Mulai Tidak Boleh Kosong " & vbNewLine
        If TANGGAL_AKHIR.Text = "" Then Pesan += "- Tanggal Akhir Tidak Boleh Kosong " & vbNewLine
        If Pesan <> "" Then
            Validasi = "Gagal menyimpan data : " & vbNewLine & vbNewLine & Pesan
        End If
        Return Validasi
    End Function
    Sub LoadNow()
        Me.TugasTableAdapter.Fill(Me.DS_t01_tugas.tugas)
        'Dim myConnString As String
        'myConnString = "server=localhost;User Id=root;password=;Persist Security Info=True;database=db_petruk_new"
        'Dim ds As New DataSet
        'Dim dsconn As New MySqlConnection(myConnString)
        'Dim dsDa As New MySqlDataAdapter
        'Dim strsql As String = "SELECT tugas.TGL_AWAL,tugas.TGL_AKHIR,master_sopir.NAMA,master_truk.PLAT_NOMOR,master_titik.NAMA_TPS FROM tugas JOIN master_sopir ON master_sopir.ID_SOPIR=tugas.ID_SOPIR JOIN tugas_tps ON tugas_tps.ID_TUGAS=tugas.ID_TUGAS JOIN master_titik ON master_titik.ID_TITIK=tugas_tps.ID_TITIK JOIN master_truk ON master_truk.ID_TRUK = master_sopir.ID_TRUK WHERE TGL_AWAL >= '2014-02-01' AND TGL_AKHIR <= '2014-02-28'"
        'Try
        '    dsconn.Open()
        '    dsDa.SelectCommand = New MySqlCommand(strsql, dsconn)
        '    dsDa.Fill(ds)
        '    ds.WriteXml(CurDir() & "/rekap_penjadwalan.xml", XmlWriteMode.WriteSchema)
        'Catch ex As Exception
        '    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        'End Try
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SIMPAN_BTN.Click
        Dim myConnString As String
        myConnString = "server=localhost;User Id=root;password=;Persist Security Info=True;database=db_petruk_new"
        Dim ds As New DataSet
        Dim dsconn As New MySqlConnection(myConnString)
        Dim strsql As String = ""

        Try
            If Form_Valid() = "" Then
                If GRUP.Text = "" Or GRUP.Text = "-1" Then
                    strsql = "INSERT INTO tugas (TGL_AWAL,TGL_AKHIR,GRUP) VALUES ('" & TANGGAL_MULAI.Text & "','" & TANGGAL_AKHIR.Text & "','" & GRUP.Text & "')"
                Else
                    strsql = "UPDATE tugas SET TGL_AWAL = '" & TANGGAL_MULAI.Text & "', TGL_AKHIR = '" & TANGGAL_AKHIR.Text & "',GRUP = '" & GRUP.Text & "' WHERE ID_SOPIR = '" & GRUP.Text & "'"
                End If
                Dim dsDa As New MySqlCommand(strsql, dsconn)
                dsconn.Open()
                dsDa.ExecuteNonQuery()
                'Me.Validate()
                MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
                LoadNow()
                clearForm()
            Else
                MsgBox(Form_Valid, MsgBoxStyle.Critical, "Error Data Saving")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub TugasDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TugasDataGridView.CellClick
        TANGGAL_MULAI.Text = TugasDataGridView.Rows(e.RowIndex).Cells(2).Value.ToString()
        TANGGAL_AKHIR.Text = TugasDataGridView.Rows(e.RowIndex).Cells(3).Value.ToString()
        GRUP.Text = TugasDataGridView.Rows(e.RowIndex).Cells(4).Value.ToString()
    End Sub

    Private Sub TugasDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TugasDataGridView.CellContentClick
        On Error Resume Next
        Select Case e.ColumnIndex
            Case 6
                rekap_penjadwalan.Show()
        End Select
    End Sub

    Private Sub TAMBAH_BTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TAMBAH_BTN.Click
        clearForm()
    End Sub
    Sub clearForm()
        TANGGAL_AKHIR.Text = ""
        TANGGAL_MULAI.Text = ""
        GRUP.Text = ""
    End Sub

    Private Sub TANGGAL_MULAI_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TANGGAL_MULAI.ValueChanged

    End Sub
End Class